/**
 * 质控病种列表初始化
 */
var SdsDept = {
    id: "deptTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    departId: 0,
    departName: null
};
/**
 * 科室点击事件
 */
SdsDept.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体科室。");
        return false;
    }
    SdsDept.departId = treeNode.id;
    SdsDept.departName = treeNode.name;
    //查询科室用户
    SdsUserDeptCfg.seItem = null;
    SdsUserDeptCfg.search();
};

/**
 * 表单项目表管理初始化
 */
var SdsUserDeptCfg = {
    id: "UserDeptCfgForm",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsUserDeptCfg.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '科室名称', field: 'location.description', align: 'left', valign: 'middle', sortable: true},
        {title: '用户登录名', field: 'user.loginName', align: 'left', valign: 'middle', sortable: true},
        {title: '用户名称', field: 'user.name', align: 'left', valign: 'middle', sortable: true}]
};

/**
 * 检查是否选中
 */
SdsUserDeptCfg.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsUserDeptCfg.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增表单项目表
 */
SdsUserDeptCfg.add = function () {
    if (SdsDept.departId) {
        var index = Sys.open('项目维护', Sys.backendPath + '/sds/userdeptcfg/to_add', ['800px', '480px'],function(layero, index, iframeWin){
            $(iframeWin.document).find("#departId").attr("value", SdsDept.departId);
        });
        this.layerIndex = index;
    } else {
        Sys.info("请先选中科室！");
    }

};

/**
 * 打开查看表单项目表详情
 */
SdsUserDeptCfg.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('表单项目表详情',Sys.backendPath + '/sds/SdsUserDeptCfg/to_update/' + id,['800px', '480px']);
        this.layerIndex = index;
    }
};

/**
 * 作废表单项目表
 */
SdsUserDeptCfg.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/SdsUserDeptCfg/setInvalidStatus", function (data) {
                Sys.success("作废成功!");
                SdsUserDeptCfg.table.refresh();
            }, function (data) {
                Sys.error("作废失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsQcformItemId",id);
            ajax.start();
        };
        Sys.confirm("是否作废该表单项目?",operation);
    }
};

/**
 * 查询表单项目表列表
 */
SdsUserDeptCfg.creatFormHtmlField = function () {
    if (SdsDept.departId) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/SdsUserDeptCfg/creatFormHtmlField", function (rd) {
                var showForm = function (){
                    var index = layer.open({
                        type: 2,
                        title: '病种名称：'+SdsDept.departName,
                        area: ['1000px', '500px'], //宽高
                        fix: false, //不固定
                        maxmin: true,
                        content: Sys.backendPath + "/sds/SdsUserDeptCfg/to_form/"+rd
                    });
                    this.layerIndex = index;
                }
                Sys.confirm("查看表单模板?",showForm);
            }, function (data) {
                Sys.error("更新失败!" + data.responseJSON.message + "!");
            });
            ajax.set("departId",SdsDept.departId);
            ajax.start();
        };
        Sys.confirm("确认更新表单模板?",operation);
    } else {
        Sys.info("请先选中某一病种！");
    }
};

/**
 * 查询表单项目表列表
 */
SdsUserDeptCfg.search = function () {
    var queryData = {};
    queryData['departId'] = SdsDept.departId;
    SdsUserDeptCfg.table.refresh({query: queryData});
};

$(function () {
    //查询科室树
    var ztree = new $ZTree("deptTree", Sys.backendPath + "/sds/userdeptcfg/deptTree");
    ztree.bindOnClick(SdsDept.onClickEntity);
    ztree.init();
    SdsDept.ztree = ztree;
    fuzzySearch('deptTree','#seachKey',null,true);

    var formColumns = SdsUserDeptCfg.initColumn();
    var formTable = new BSTable(SdsUserDeptCfg.id, Sys.backendPath + "/sds/userdeptcfg/list", formColumns);
    formTable.setPaginationType("server");
    formTable.setPageSize(10);
    formTable.setSubHeight(-10);
    formTable.setShowExport(false);
    formTable.setShowRefresh(false);
    formTable.setShowToggle(false);
    formTable.setShowColumns(false);
    formTable.setQueryParams({"departId":0})
    SdsUserDeptCfg.table = formTable.init();
});
